package org.luxor.commons.core.utils;

import java.util.Random;

/**
 * 随机数生成器
 *
 * @author admin @date 2019/9/15 20:23 @version 0.0.1
 */
public class RandomUtils {
    private static final char[] CHAR_ARRAYS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();

    /**
     * 获取指定范围内的一个随意数
     */
    public static int randomNumber(int min, int max) {
        return min + (int) (Math.random() * ((max - min) + 1));
    }

    /**
     * 生成固定长度的整数串
     */
    public static String randomNumber(int length) {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    /**
     * 生成固定长度的字符串
     */
    public static String randomString(int length) {
        return randomString(CHAR_ARRAYS, length);
    }

    public static String randomString(char[] charArrays, int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int x = 0; x < length; ++x) {
            sb.append(charArrays[random.nextInt(charArrays.length)]);
        }
        return sb.toString();
    }
}
